package com.androidcourse.shounews.HttpUtil;

import static android.content.ContentValues.TAG;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.androidcourse.shounews.Entity.CommonNews;
import com.androidcourse.shounews.Entity.MyNews;
import com.androidcourse.shounews.Entity.ResultMap;
import com.androidcourse.shounews.Toast.MyCustomToast;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import okhttp3.Call;
import okhttp3.HttpUrl;

public class NewsHttp {
    public static String baseLocalUrl="https://www.zouran.top/android";

    private static String getAllNewsByAuthor="/news/getAllNewsByAuthor";
    private static String getAllNewsCount="/news/getAllCount";

    private static String getAllLink="/link/getAllLink";
    private static String addWechatNews="/news/addNews/wechat";
    public static String getImg="/news/img";
    private static String getAllAuthorByType="/identity/getAllAuthorByType";
    /**
     * @param author 作者名称
     * @return CompletableFuture 新闻List
     */
    public static CompletableFuture<List<CommonNews>> getAllNewsByAuthor(String author){
        HttpUrl.Builder builder=HttpUrl.parse(baseLocalUrl+getAllNewsByAuthor)
                .newBuilder().addQueryParameter("author",author);
        String url=builder.build().toString();
        MyOkHttp myOkHttp=new MyOkHttp();
        return myOkHttp.http(url)
                .thenApply(res->{
                    List<CommonNews> commonNewsList=new ArrayList<>();
                    JSONArray array=JSONObject.parseObject(res).getJSONArray("data");
                    for (Object object : array) {
                        JSONObject o= JSON.parseObject(object.toString());
                        CommonNews commonNews=new CommonNews(o.get("id").toString(),o.get("title").toString(),o.get("author").toString(),
                                o.get("time").toString(),o.get("link").toString(),o.get("img")==null?"1":o.get("img").toString());
                        commonNewsList.add(commonNews);
                    }
                    return commonNewsList;});
    }

    /**
     * @param url 微信文章链接
     * @return 添加信息
     */
    public static CompletableFuture<ResultMap> addWechatNews(String url) {
        HttpUrl.Builder builder=HttpUrl.parse(baseLocalUrl+addWechatNews)
                .newBuilder().addQueryParameter("url",url);
        MyOkHttp myOkHttp=new MyOkHttp();
        return myOkHttp.http(builder.build().toString()).thenApply(res->{
            JSONObject object=JSON.parseObject(res);
            ResultMap resultMap=new ResultMap(Integer.parseInt(object.get("code").toString()),object.get("message").toString(),object.get("data")==null?"":object.get("data").toString());
            return resultMap;
        });
    }

    /**
     * @param type 文章类型
     * @return 所有作者
     */
    public static CompletableFuture<List<String>> getAllAuthorByType(String type) {
        HttpUrl.Builder builder=HttpUrl.parse(baseLocalUrl+getAllAuthorByType)
                .newBuilder().addQueryParameter("name",type);
        MyOkHttp myOkHttp=new MyOkHttp();
        return myOkHttp.http(builder.build().toString()).thenApply(res->{
            List<String> list=new ArrayList<>();
            JSONArray array=JSONObject.parseObject(res).getJSONArray("data");
            for (Object o : array) {
                list.add(o.toString());
            }
            return list;
        });
    }

    /**
     * @return 数组
     */
    public static CompletableFuture<List<MyNews>> getAllLink() {
        HttpUrl.Builder builder=HttpUrl.parse(baseLocalUrl+getAllLink)
                .newBuilder();
        MyOkHttp myOkHttp=new MyOkHttp();
        return myOkHttp.http(builder.build().toString()).thenApply(res->{
            List<MyNews> list=new ArrayList<>();
            JSONArray array=JSONObject.parseObject(res).getJSONArray("data");
            for (Object object : array) {
                JSONObject o= JSON.parseObject(object.toString());
                list.add(new MyNews(o.get("name").toString(), o.get("link").toString()));
            }
            return list;
        });
    }

    /**
     * @return 总的消息数量
     */
    public static CompletableFuture<Integer> getAllNewsCount() {
        HttpUrl.Builder builder=HttpUrl.parse(baseLocalUrl+getAllNewsCount)
                .newBuilder();
        MyOkHttp myOkHttp=new MyOkHttp();
        return myOkHttp.http(builder.build().toString()).thenApply(res->{
            String count=JSON.parseObject(res).get("data").toString();
            return Integer.parseInt(count);
        });
    }
}
